package com.sandy.main;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class Newuser
 */
public class Newuser extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Newuser() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String uname = (String)request.getParameter("name");
		String email = (String)request.getParameter("email");
		
		String driver = "com.mysql.jdbc.Driver";
		String un = "root";
		String pwd = "sandeep";
		String url = "jdbc:mysql://localhost:3306";
		
		Connection con = null;
		//String hash;
		//double password;
		//Random rand = new Random();
		
		//hash = md5( rand.nextInt(1000) );
		
		//password = 1000 + (Math.random() * (5000 - 1000));
		
		//Random();
		String password = new PasswordGenerator().generate();
		
		String hash = new Md5HexGenerator().generate();
		
		System.out.println("password= " +password);
		System.out.println("hash= " +hash);
		try {
			Class.forName(driver);
			System.out.println("driver");
			con = DriverManager.getConnection(url, un, pwd);
			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement("insert into sandy.user(username, password, email, hash) values(?,?,?,?)");
			
            pstmt.setString(1,uname);
			pstmt.setString(2,password);
			pstmt.setString(3,email);
			pstmt.setString(4,hash);
			int i=pstmt.executeUpdate();
			System.out.println("1 row updated");
		
	}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		System.out.println("entering mail section");
		
		response.setContentType("text/html");  
	       PrintWriter out = response.getWriter();  
	  
	           final String userName = "burnthenight123@gmail.com";  
	       final String password2 = "burnthenight";  
	          
	        Properties prop = System.getProperties();  
	        prop.put("mail.smtp.auth","true");  
	        prop.put("mail.smtp.strttls.enable","true");  
	        prop.put("mail.smtp.starttls.enable", "true");
	        prop.put("mail.smtp.host","smtp.gmail.com");  
	        prop.put("mail.smtp.port","587");  
	        //prop.put("","");  
	          
	        Session session = Session.getDefaultInstance(prop,new javax.mail.Authenticator(){  
	            protected PasswordAuthentication getPasswordAuthentication(){  
	                return new PasswordAuthentication(userName, password2);   
	            }  
	        });  
	                  
	        try{  
	          
	            MimeMessage msg = new MimeMessage(session);  
	            msg.setFrom(new InternetAddress(userName));  
	            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(email));  
	            msg.setSubject("Verify Account");  
	            msg.setText("hello u have been registerd to www.sandy.com website \nusername=" +uname+"\npassword="+password+ "\nplease click the link to acticate your account:\n" +
	            		"				 http://localhost:8080/verify.jsp?email="+email+"hash="+hash);
	           
	            
	            Transport.send(msg);  
	            System.out.println("msg sent successfully.!");  
	              
	            }catch (MessagingException mex) {  
	            mex.printStackTrace();  
	        }  
		
		//System.out.println("Data has been inserted in to Database");
		//System.out.println("Number of rows inserted is:"+i);
		request.setAttribute("name1", uname);
		request.setAttribute("email1", email);
		RequestDispatcher move=request.getRequestDispatcher("newuser.jsp");
		move.forward(request, response);
		
	}

}
